#include <iostream>
#include <vector>

using namespace std;

// https://leetcode.cn/problems/candy/description/?envType=daily-question&envId=2025-06-02
// 135. 分发糖果

class Solution {
public:
    int candy(vector<int>& ratings) {
        int n = ratings.size();
        int ans = n;
        for (int i = 0; i < n; i++){
            i = i > 0 && ratings[i - 1] < ratings[i] ? i - 1 : i;
            // 上坡
            int up = 0;
            int down = 0;
            int top = 0;
            up = i;
            while (i + 1 < n && ratings[i] < ratings[i + 1])
            {
                i++;
            }
            top = i;
            // 下坡
            while(i + 1 < n && ratings[i] > ratings[i + 1]){
                i++;
            }
            down = i;

            int inc = top - up;
            int dec = i - top;
            ans += (inc * (inc - 1) + (dec - 1) * dec) / 2 + max(inc, dec);
        }
        return ans;
    }
};

int main()
{
    Solution solution{};
    vector<int> nums{1, 3, 2, 2, 1};
    cout << solution.candy(nums) << endl;
    return 0;
}